/*
 * Created on 2005-6-6
 * Last modified on 2007-1-17
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.util;

import java.util.HashMap;

import org.apache.log4j.Logger;

/**
 * JAVA类型转换
 * @author YeQiangWei by 我容易么我
 * Powered by YeQiangWei.com
 */
public class TypeChange {
	
	private static Logger logger = Logger.getLogger(TypeChange.class.getName());
	
	public static HashMap objToHashMap(Object o){
		try{
	    	return (HashMap)o;
    	}catch(Exception e){
    		logger.error(e.toString());
    		return null;
    	}	
	}
	
	public static int objToInt(Object o){
		if(o==null){return 0;}
		int i = 0;
	    try{
    		String s = String.valueOf(o);
    		i = TypeChange.stringToInt(s);
    	}catch(Exception e2){
    		logger.error(e2.toString());
    		i = 0;
    	}
	    return i;
	}
	
	public static double objToDouble(Object o){
		double d = 0;
	    if(o==null){
	    	d = 0;
	    }else{
	    	try{
	    		Double du = (Double)o;
		    	d = du.doubleValue();
	    	}catch(Exception e){
	    		logger.error(e.toString());
	    		d = 0;
	    	}
	    }
	    return d;
	}
	
	public static short objToShort(Object o){
		short s = 0;
	    if(o==null){
	    	s = 0;
	    }else{
	    	try{
	    		Short du = (Short)o;
		    	s = du.shortValue();
	    	}catch(Exception e){
	    		logger.error(e.toString());
	    		s = 0;
	    	}
	    }
	    return s;
	}
	
	public static String objOfString(Object o){
		String str = "";
	    if(o==null){
	        str = "";
	    }else{
	    	str = String.valueOf(o);
	    }
	    return str;
	}
	
	public static boolean objToBoolean(Object o){
		boolean b = false;
	    if(o!=null){
	    	try{
	    		Boolean str = null;
	    		str = (Boolean)o;
	    		b = str.booleanValue();
	    	}catch(Exception e){
	    		logger.error(e.toString());
	    	}
	    }
	    return b;
	}
	
	public static String nullOfString(String str){
	    if(str==null){
	        str = "";
	    }
	    return str;
	}
    
    public static byte stringToByte(String str){
        byte b = 0;
        if(str!=null){
            try{
                b = Byte.parseByte(str);
            }catch(Exception e){
            	logger.error(e.toString());
            }
        }
        return b;
    }
    
    public static boolean stringToBoolean(String str){
        if(str==null){
            return false;
        }else{
            if(str.equals("1")){
                return true;
            }else if(str.equals("0")){
                return false;
            }
            else if(str.equals("true")){
                return true;
            }
            else if(str.equals("false")){
                return false;
            }
            else{
                try{
                    return Boolean.parseBoolean(str);
                }catch(Exception e){
                	logger.error(e.toString());
                    return false;
                }
            }
        }
    }

    public static long stringToLong(String str) {
    	if(Validator.isEmpty(str)){
    		return 0;
    	}else{
    		str = str.trim();
    	}
        Long li = new Long(0);
        try{
            li = Long.parseLong(str);
        }catch(Exception e){
        	logger.error(e.toString());
        }
        return li.longValue();
    }
    
	public static int stringToInt(String str){
		if(Validator.isEmpty(str)){
			return 0;
		}
		return stringToInt(str.trim(),0);
	}
	
	public static int stringToInt(String str, int i2){
		int i = 0;
		if(!Validator.isEmpty(str)&&Utils.isNumber(str)){
			try{
				i = Integer.parseInt(str.trim());
			}catch(Exception e){
				logger.error(e.toString());
				i = i2;
			}
		}else{
			i = i2;
		}
		return i;
	}
	

    public static short stringToShort(String str){
        short i=0;
        if(str!=null&&Utils.isNumber(str)){
            try{
                i = Short.parseShort(str.trim());
            }catch(Exception e){
            	logger.error(e.toString());
                i = 0;
            }
        }else{
            i = 0;
        }
        return i;
    }
    
    
	public static double stringToDouble(String str){
		double i = 0;
		if(!Utils.isNull(str)){
			try{
				i = Double.parseDouble(str.trim());
			}catch(Exception e){
				logger.error(e.toString());
			}
		}else{
			i = 0;
		}      
		return i;
	}
	
	public static String intToString(int i){
		String str = String.valueOf(i);
		return str;
	}
    
    
	public static long doubleToLong(double d){
	    long lo=0;
	    try{
	        //double转换成long前要过滤掉double类型小数点后数据
	        lo = Long.parseLong(String.valueOf(d).substring(0,String.valueOf(d).lastIndexOf(".")));
	    }catch(Exception e){
	    	logger.error(e.toString());
	        lo=0;
	    }
	    return lo;	    
	}
    
    public static int doubleToInt(double d){
        int i=0;
        try{
            //double转换成long前要过滤掉double类型小数点后数据
            i = Integer.parseInt(String.valueOf(d).substring(0,String.valueOf(d).lastIndexOf(".")));
        }catch(Exception e){
        	logger.error(e.toString());
            i=0;
        }
        return i;      
    }
    
    public static double longToDouble(long d){
        double lo=0;
        try{
            lo = Double.parseDouble(String.valueOf(d));
        }catch(Exception e){
        	logger.error(e.toString());
            lo = 0;
        }
        return lo;      
    }
    
    public static int longToInt(long d){
        int lo=0;
        try{
            lo = Integer.parseInt(String.valueOf(d));
        }catch(Exception e){
        	logger.error(e.toString());
            lo=0;
        }
        return lo;      
    }


    public static String longToString(long li) {
        String str = "";
        try{
            str = String.valueOf(li); 
        }catch(Exception e){
        	logger.error(e.toString());
        }
        return str;
    }
    
}
